<template>
    <div class="app-container">
      <projectHeader @tabClick="tabClick"></projectHeader>
        <!-- 查询条件 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
            <el-form-item label="设备名称或序列号" prop="keyword">
              <el-input
                  v-model="queryParams.keyword"
                  placeholder="请输入设备名称或序列号"
                  clearable
                  style="width: 200px"
                  @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-form-item label="充电站" prop="stationId">
              <el-select
                  v-model="queryParams.stationId"
                  clearable
                  placeholder="请选择"
                  style="width: 200px"
              >
                  <el-option
                    v-for="item in stationOptions"
                    :key="item.id"
                    :label="item.label"
                    :value="item.id"
                  />
              </el-select>
            </el-form-item>
            <el-form-item label="设备类型" prop="deviceType">
                <el-select style="width: 200px" v-model="queryParams.deviceType" clearable placeholder="请选择">
									<el-option
										v-for="item in dict.type.charg_device_type"
										:key="item.value"
										:label="item.label"
										:value="parseInt(item.value)"
									/>
                </el-select>
            </el-form-item>
						<el-form-item label="运行状态" prop="runStatus">
						    <el-select style="width: 200px" v-model="queryParams.runStatus" clearable placeholder="请选择">
									<el-option
										v-for="item in dict.type.charg_run_status"
										:key="item.value"
										:label="item.label"
										:value="parseInt(item.value)"
									/>
						    </el-select>
						</el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 操作 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
                v-hasPermi="['system:eaChargingPile:add']"
            >新增
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!-- 方式三 -->
				<el-table v-loading="loading" :data="list" >
					<el-table-column label="充电桩名称" align="center" prop="pileName"/>
					<el-table-column label="设备序列号" align="center" prop="code"/>
					<el-table-column label="厂商-型号" align="center" prop="chargingTypeName"/>
					<el-table-column label="充电站" align="center" prop="stationName"/>
					<el-table-column label="充电口" align="center" prop="create_time">
            <template slot-scope="{row}">
              <el-link type="primary" @click="handChongdian(row)">查看所属充电</el-link>
            </template>
					</el-table-column>
					<el-table-column label="状态" align="center" prop="create_time">
            <template slot-scope="{row}">
              <span style="color: #85CE61;font-size: 16px;" v-if="row.runStatus == 1">在线</span>
              <span style="color: #F57879;font-size: 16px;" v-else-if="row.runStatus == 0">离线</span>
              <span style="font-size: 16px;" v-else>未知</span>
            </template>
					</el-table-column>
					<el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
            <template slot-scope="{row}">
              <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)"
                v-hasPermi="['system:eaChargingPile:edit']">编辑设备
              </el-button>
              <el-button type="text" icon="el-icon-orange" @click="handleShowDetail(row)"
                v-hasPermi="['system:eaChargingPile:query']">设备详情
              </el-button>
              <el-button link type="text" icon="el-icon-s-order" @click="handleShowOrder(1, row)"
                v-hasPermi="['system:eaOrder:list']">查看订单
              </el-button>
            </template>
					</el-table-column>
				</el-table>

       <pagination
         v-show="total>0"
         :total="total"
         :page.sync="queryParams.pageNum"
         :limit.sync="queryParams.pageSize"
         @pagination="getList"
       />

        <!-- 添加或修改设备类型对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
					<div v-if="title == '充电口列表'">
						<el-table :data="gunList" >
              <el-table-column label="id" align="center" prop="id"></el-table-column>
							<el-table-column label="充电口" align="center" prop="port">
								<template slot-scope="{row}">
									p{{row.port}}
								</template>
							</el-table-column>
							<el-table-column label="工作状态" align="center" prop="runStatus">
								<template slot-scope="{row}">
									<dict-tag :options="dict.type.equip_work_status" :value="row.runStatus" />
								</template>
							</el-table-column>
							<el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
								<template slot-scope="{row}">
									<el-button v-hasPermi="['system:eaOrder:list']" type="text" @click="handleShowOrder(2, row)">查看订单</el-button>
								</template>
							</el-table-column>
						</el-table>
					</div>
            <el-form v-else ref="formRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="所属充电站" prop="stationId">
                  <el-select
                      v-model="form.stationId"
                      clearable
                      placeholder="请选择"
                      style="width: 200px"
                  >
                      <el-option
                          v-for="item in stationOptions"
                          :key="item.id"
                          :label="item.label"
                          :value="item.id"
                      />
                  </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="0">禁用</el-radio>
                        <el-radio :label="1">启用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="充电桩编码" prop="code">
                    <el-input v-model="form.code" style="width: 200px" placeholder="请输入充电桩编码"/>
                </el-form-item>
                <el-form-item label="充电桩名称" prop="pileName">
                    <el-input v-model="form.pileName" style="width: 200px" placeholder="请输入充电桩名称"/>
                </el-form-item>
                <el-form-item label="所属厂商"  prop="manufacturerType">
                  <el-select
                    @change="changschange"
                    :disabled="form.id ? true : false"
                    v-model="form.manufacturerType"
                    placeholder="请选择所属厂商"
                    clearable
                  >
                    <el-option
                        v-for="dict in dict.type.equip_type_manufact"
                        :key="dict.value"
                        :label="dict.label"
                        :value="Number(dict.value)"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item label="设备型号" prop="modelId">
                  <el-select
                      v-model="form.modelId"
                      :disabled="form.id ? true : false"
                      placeholder="请选择设备型号"
                      clearable
                  >
                    <el-option
                        v-for="dict in deviceTypeOptions"
                        :key="dict.id"
                        :label="dict.label"
                        :value="dict.id"
                    />
                  </el-select>
                </el-form-item>
                <!-- <el-form-item label="端口数参数" prop="portNumber">
                    <el-input :disabled="form.id" v-model="form.portNumber" style="width: 200px" placeholder="请输入端口数参数"/>
                </el-form-item>
                <el-form-item label="设备类型" prop="deviceType">
                    <el-radio-group v-model="form.deviceType">
                        <el-radio
												v-for="(item,index) in dict.type.charg_device_type" :key="index"
												:label="Number(item.value)">{{item.label || ''}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="协议类型" prop="protocolType" style="width: 100%;">
                  <el-select
                      v-model="form.protocolType"
                      placeholder="请选择协议类型"
                      clearable
                      style="width: 240px"
                  >
                    <el-option
                        v-for="dict in typeList"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                  </el-select>
                </el-form-item> -->
                <!-- <el-form-item label="通讯协议">
                    <el-radio-group v-model="form.protocol_type">
                       <el-radio label="zdl_2_9_2">中电联2.9.2</el-radio>
                       <el-radio label="yfy_1_8">英飞园1.8</el-radio>
                        <el-radio label="ykc_1_5">云快充1.5</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="Sim卡号">
                    <el-input v-model="form.pile_sim_mac" placeholder="请输入Sim卡号"/>
                </el-form-item> -->
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" v-if="title != '充电口列表'" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">{{title != '充电口列表' ? '取 消' : '确 定'}}</el-button>
                </div>
            </template>
        </el-dialog>

        <!-- 订单 -->
        <el-dialog title="查看订单" :visible.sync="order_dialog_visible" class="order-list" width="90%" append-to-body >
            <!-- 查询条件 -->
            <OrderList ref="OrderList" v-if="order_dialog_visible" :key="order_by_charging_id" :pileId="order_by_charging_id" :gunId="order_by_gun_id" />
        </el-dialog>

        <!-- 订单 -->
        <el-dialog title="设备详情" :visible.sync="detail_dialog_visible" width="70%" append-to-body >
            <!-- 查询设备详情 -->
            <Detail v-if="detail_dialog_visible" :key="current_pile_id" :id="current_pile_id" />
        </el-dialog>
    </div>
</template>

<script name="ChargingPile">
import * as api from "@/api/station/charging_pile";
import {setForm} from "@/utils/ruoyi";
import {stationOptionSelect} from "@/api/station/station";
import {eaCarChargingTypeList} from "@/api/station/device_type";
// import useUserStore from "@/store/modules/user";
import OrderList from "@/components/Common/order/OrderList.vue";
import Detail from "@/views/station/charging_pile/components/Detail.vue";

export default {
  name: "Tag",
  dicts: ['charg_device_type','charg_run_status','equip_type_manufact','equip_work_status'],
  components: { OrderList,Detail },
  data() {
    return {
      form:{},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      queryParams: {// 查询参数
        pageNum: 1,
        pageSize: 10,
        runStatus:null,
        deviceType:null,
        keyword:'',
        stationId:null,
      },
      // 总条数
      total: 0,
      rules: {
        portNumber:[{ required: true, message: '端口数参数不能为空', trigger: 'blur' },],
        code:[{ required: true, message: '充电桩编码不能为空', trigger: 'blur' },],
        deviceType:[{ required: true, message: '请选择设备类型', trigger: 'change' },],
        pileName:[{ required: true, message: '充电桩名称不能为空', trigger: 'blur' },],
        stationId:[{ required: true, message: '请选择充充电站', trigger: 'change' },],
        status:[{ required: true, message: '请选择状态', trigger: 'change' },],
        protocolType:[{ required: true, message: '请选择协议类型', trigger: 'change' },],
        manufacturerType:[{ required: true, message: '请选择所属厂商', trigger: 'change' },],
        modelId:[{ required: true, message: '请选择设备型号', trigger: 'change' },],
      },
      list:[],//页面数据
      stationOptions:[],//所属充电站
      deviceTypeOptions:[],//充电桩类型
      current_pile_id:'',//详情id
      order_by_gun_id:'',//订单id
      order_by_charging_id:'',//订单充电id
      order_dialog_visible:false,//查看订单弹窗
      detail_dialog_visible:false,//设备详情弹窗
      plateform:{},//选择的平台项目
      gunList:[],
      eltypexyList:[
        {label:' 驴充充电瓶车',value:'LCC_BIKE'},
      ],//二轮协议类型数据
      sltypexyList:[
        {label:'云快充汽车',value:'YKC_1_6'},
        {label:'驴充充汽车',value:'LCC_CAR'},
      ],//四轮协议类型数据
      typeList:[],//协议类型数据
    };
  },
  created() {
    // this.getAllDeviceType()
  },
  methods: {
    tabClick(e){//平台切换
      this.plateform = {...e}
      if(e.value == 2) {
        this.typeList = [...this.eltypexyList]
      }else{
        this.typeList = [...this.sltypexyList]
      }
      this.queryParams.pageNum = 1
      this.getList()
      let queryParams = {...this.queryParams}
      queryParams.stationId = null
      this.queryParams = {...queryParams}
      this.getAllStation()
    },
    getList() {
      this.loading = true;
      this.queryParams.platformType = this.plateform.value
      api.listChargingPile(this.queryParams).then(response => {
        this.list = response.rows || [];
        this.total = response.total || 0;
        this.loading = false;
      });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.open = true;
      this.reset();
      this.title = "添加设备管理";
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.open = true;
      this.title = "修改设备管理";
      api.getChargingPile(id).then(response => {
        let data = response.data || {}
        let chargingModelVo = data.chargingModelVo || {}
        this.form = {...data,manufacturerType:chargingModelVo.manufacturerType || '',modelId:chargingModelVo.id || ''};
        if(chargingModelVo.manufacturerType) this.getAllDeviceType(chargingModelVo.manufacturerType)
      });
    },
    handChongdian(row){
    	this.title = '充电口列表'
    	this.open = true;
      api.getChargingPile(row.id).then(response => {
        let data = response.data || {}
        let portList = data.portList || []
        this.gunList = [...portList]
      });
    },
    submitForm() {
      this.$refs["formRef"].validate(valid => {
        if (valid) {
          let form = {...this.form}
          form.platformType = this.plateform.value
          if (this.form.id != undefined) {
            delete form.chargingTypeId
            api.updateChargingPile(form).then(response => {
                this.$modal.msgSuccess("修改成功")
                this.open = false;
                this.getList();
            });
          } else {
            api.addChargingPile(form).then(response => {
                this.$modal.msgSuccess("新增成功")
                this.open = false;
                this.getList()
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {periodPriceList:[{}]};
      this.deviceTypeOptions = []
      this.resetForm("formRef");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    changschange(e){//厂商改变
      let form = {...this.form}
      form.modelId = null
      this.form = {...form}
      console.log(e)
      this.getAllDeviceType(e)
    },
    getAllStation() {//获取充电设备类型
      stationOptionSelect({platformType:this.plateform.value}).then(resp => {
        this.stationOptions = resp.data || [];
      })
    },
    getAllDeviceType(tyype) {//充电桩类型
      eaCarChargingTypeList({manufacturerType:tyype,platformType:this.plateform.value}).then(resp => {
        this.deviceTypeOptions = resp.data || [];
      })
    },
    handleShowOrder(type, row) {//查看订单
      console.log('handleShowOrder', type, row)
      this.order_by_charging_id = 0
      this.order_by_gun_id = 0
      if (row) {
        if (type == 1) {
            this.order_by_charging_id = row.id
        } else if(type == 2) {
           this.order_by_gun_id = row.id
        }
      }
      this.order_dialog_visible = true;
      this.$nextTick(()=>{
        this.$refs.OrderList.tabClick(this.plateform)
      })
    },
    handleShowDetail(row) {
      this.current_pile_id = row.id
      this.detail_dialog_visible = true;
    },
    getOrderList() {//查询订单列表
      this.loading = true;
      // api.listChargingPileOrder(proxy.addDateRange(orderQueryParams.value, orderDateRange.value)).then(response => {
      //     orderList.value = response.rows;
      //     orderTotal.value = response.total;
      //     orderListLoading.value = false;
      // });
    },
    handleDelete(row) {//删除
      const del_ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + del_ids + '"的数据项？').then(function() {
        return api.delChargingPile(del_ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};

</script>

<style lang="scss" scoped>
.order-list {
	.el-col {
		text-align: center;
	}
}
</style>
